package com.gs.m_home.persistent

import android.content.Context
import android.os.Handler
import android.os.Looper
import com.gs.common.constant.StateCode
import com.gs.m_home.callback.OnDoGetUserinfoStateCallback
import com.gs.m_home.callback.OnDoLogoutStateCallback
import com.gs.m_home.model.MineModel

class MinePersistent(context: Context) {

    private val mineModel by lazy { MineModel.getInstance(context) }
    private val handler = Handler(Looper.getMainLooper())

    fun userLogout(callback: OnDoLogoutStateCallback) {
        Thread{
            mineModel.logout {
                when (it) {
                    StateCode.STATE_LOADING -> {
                        handler.post {
                            callback.onLogoutLoading()
                        }
                    }
                    StateCode.STATE_SUCCESS -> {
                        handler.post {
                            callback.onLogoutSuccess()
                        }
                    }
                    StateCode.STATE_FAILED -> {
                        handler.post {
                            callback.onLogoutFailed()
                        }
                    }
                    StateCode.NETWORK_ERROR -> {
                        handler.post {
                            callback.onNetworkError()
                        }
                    }
                    StateCode.SERVER_ERROR -> {
                        handler.post {
                            callback.onServerError()
                        }
                    }
                    StateCode.UNKNOWN_ERROR -> {
                        handler.post {
                            callback.onUnknownError()
                        }
                    }
                }
            }
        }.start()
    }

    fun getUserInfo(callback: OnDoGetUserinfoStateCallback) {
        Thread{
            mineModel.getUserInfo {
                when (it) {
                    StateCode.STATE_LOADING -> {
                        handler.post {
                            callback.onGetUserinfoLoading()
                        }
                    }
                    StateCode.STATE_SUCCESS -> {
                        handler.post {
                            callback.onGetUserinfoSuccess()
                        }
                    }
                    StateCode.STATE_FAILED -> {
                        handler.post {
                            callback.onGetUserinfoFailed()
                        }
                    }
                    StateCode.NETWORK_ERROR -> {
                        handler.post {
                            callback.onNetworkError()
                        }
                    }
                    StateCode.SERVER_ERROR -> {
                        handler.post {
                            callback.onServerError()
                        }
                    }
                    StateCode.UNKNOWN_ERROR -> {
                        handler.post {
                            callback.onUnknownError()
                        }
                    }
                }
            }
        }.start()
    }

    companion object{
        private var instance: MinePersistent? = null
        fun getInstance(context: Context): MinePersistent {
            if (instance == null) {
                synchronized(this::class) {
                    if (instance == null) {
                        instance = MinePersistent(context)
                    }
                }
            }
            return instance!!
        }
    }
}